import {Collection, Document, Grid, House, Key, Setting, User} from '@element-plus/icons-vue'

export interface MenuItemConfig {
  path: string
  title: string
  icon?: any
  children?: MenuItemConfig[]
  // 权限控制 - 哪些角色可以访问
  roles?: string[]
  // 是否在首页显示为快速访问
  showInQuickAccess?: boolean
}

// 菜单配置
export const menuConfig: MenuItemConfig[] = [
  {
    path: '/',
    title: '首页',
    icon: House,
    roles: ['ADMIN', 'SUPER_ADMIN', 'DEVELOPER', 'VIEWER'],
    showInQuickAccess: false
  },
  {
    path: '/access',
    title: '用户与权限',
    icon: Setting,
    roles: ['ADMIN', 'SUPER_ADMIN'],
    showInQuickAccess: false,
    children: [
      {
        path: '/access/users',
        title: '用户管理',
        icon: User,
        roles: ['ADMIN', 'SUPER_ADMIN']
      },
      {
        path: '/access/permissions',
        title: '产品权限管理',
        icon: Key,
        roles: ['ADMIN', 'SUPER_ADMIN']
      }
    ]
  },
  {
    path: '/data',
    title: '元数据管理',
    icon: Document,
    roles: ['ADMIN', 'SUPER_ADMIN', 'DEVELOPER', 'VIEWER'],
    showInQuickAccess: true,
    children: [
      {
        path: '/data/products',
        title: '产品管理',
        icon: Collection,
        roles: ['ADMIN', 'SUPER_ADMIN', 'DEVELOPER', 'VIEWER'],
        showInQuickAccess: true
      },
      {
        path: '/data/field-library',
        title: '字段库管理',
        icon: Grid,
        roles: ['ADMIN', 'SUPER_ADMIN', 'DEVELOPER', 'VIEWER'],
        showInQuickAccess: true
      },
      // SCHEMA - 暂时注释，等页面开发完成再启用
      // {
      //   path: '/data/schemas',
      //   title: 'SCHEMA管理',
      //   icon: DataBoard,
      //   roles: ['ADMIN', 'SUPER_ADMIN', 'DEVELOPER', 'VIEWER']
      // },
      // 表结构 - 暂时注释，等页面开发完成再启用
      // {
      //   path: '/data/tables',
      //   title: '表结构管理',
      //   icon: Grid,
      //   roles: ['ADMIN', 'SUPER_ADMIN', 'DEVELOPER', 'VIEWER']
      // },
      // 数据字典 - 暂时注释，等页面开发完成再启用
      // {
      //   path: '/data/dictionaries',
      //   title: '数据字典',
      //   icon: Lock,
      //   roles: ['ADMIN', 'SUPER_ADMIN', 'DEVELOPER', 'VIEWER']
      // }
    ]
  }
]

/**
 * 根据用户角色获取可访问的菜单项
 * @param userRole 用户角色
 * @returns 过滤后的菜单配置
 */
export const getMenusByRole = (userRole: string | null): MenuItemConfig[] => {
  if (!userRole) {
    return []
  }

  return menuConfig
    .map(item => {
      // 创建菜单项的副本
      const menuItem = { ...item }

      // 检查顶级菜单权限
      const hasTopMenuPermission = !item.roles || item.roles.includes(userRole)

      // 过滤子菜单
      if (item.children) {
        const filteredChildren = item.children.filter(child =>
          !child.roles || child.roles.includes(userRole)
        )

        // 如果顶级菜单有权限，或者有可访问的子菜单，则保留该菜单项
        if (hasTopMenuPermission || filteredChildren.length > 0) {
          menuItem.children = filteredChildren.length > 0 ? filteredChildren : undefined
          return menuItem
        }
      }

      // 如果顶级菜单有权限，直接返回
      if (hasTopMenuPermission) {
        return menuItem
      }

      return null
    })
    .filter(Boolean) as MenuItemConfig[]
}

/**
 * 获取快速访问菜单项
 * @param userRole 用户角色
 * @returns 快速访问菜单配置
 */
export const getQuickAccessMenus = (userRole: string | null): MenuItemConfig[] => {
  if (!userRole) {
    return []
  }

  // 先获取所有可访问的菜单
  const allMenus = getMenusByRole(userRole)
  const quickAccessItems: MenuItemConfig[] = []

  allMenus.forEach(menu => {
    // 顶级菜单如果标记了快速访问，且有子菜单，则添加子菜单
    if (menu.showInQuickAccess && menu.children && menu.children.length > 0) {
      menu.children.forEach(child => {
        if (child.showInQuickAccess) {
          quickAccessItems.push(child)
        }
      })
    }
  })

  return quickAccessItems
}

/**
 * 根据路径查找菜单项
 * @param path 菜单路径
 * @param menus 菜单列表
 * @returns 菜单项或null
 */
export const findMenuByPath = (path: string, menus: MenuItemConfig[]): MenuItemConfig | null => {
  for (const menu of menus) {
    if (menu.path === path) {
      return menu
    }
    if (menu.children) {
      const found = findMenuByPath(path, menu.children)
      if (found) {
        return found
      }
    }
  }
  return null
}

/**
 * 查找菜单所属的顶级菜单
 * @param path 当前路径
 * @param menus 菜单列表
 * @returns 顶级菜单或null
 */
export const findTopMenu = (path: string, menus: MenuItemConfig[]): MenuItemConfig | null => {
  for (const menu of menus) {
    // 首页特殊处理
    if (path === '/') {
      return menu.path === '/' ? menu : null
    }

    // 精确匹配：路径完全相等或以 / 分隔的子路径
    if (path === menu.path ||
        (path.startsWith(menu.path + '/') && menu.path !== '/')) {
      return menu
    }
  }
  return null
}

/**
 * 获取顶级菜单的第一个子菜单路径
 * @param topMenu 顶级菜单
 * @returns 第一个子菜单路径或null
 */
export const getFirstChildPath = (topMenu: MenuItemConfig): string | null => {
  if (topMenu.children && topMenu.children.length > 0) {
    return topMenu.children[0].path
  }
  return null
}
